
cc.Class({
    extends: cc.Component,

    properties: {
        tag :cc.Prefab
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.pathCanvas = cc.find('Canvas/canvas').getComponent('PathCanvas')
        this.setTouchEnabled(true)
        this.points = [];
        this.isControlPoint = false;
        this.points = [];
        this.chilrens = [];
    },

    setTouchEnabled(enable){
        if(enable){
            this.node.on(cc.Node.EventType.TOUCH_START,this.onTouch.bind(this));
            this.node.on(cc.Node.EventType.TOUCH_MOVE,this.onTouch.bind(this));
            this.node.on(cc.Node.EventType.TOUCH_END,this.onTouch.bind(this));
            this.node.on(cc.Node.EventType.TOUCH_CANCEL,this.onTouch.bind(this));
        } else {
            this.node.off(cc.Node.EventType.TOUCH_START,this.onTouch.bind(this));
            this.node.off(cc.Node.EventType.TOUCH_MOVE,this.onTouch.bind(this));
            this.node.off(cc.Node.EventType.TOUCH_END,this.onTouch.bind(this));
            this.node.off(cc.Node.EventType.TOUCH_CANCEL,this.onTouch.bind(this));
        }
    },

    updateTag(f){
        for(var i=0; i<this.points.length; i++){
            var p = this.points[i];
            
            if(this.chilrens.length <= i){
                var node = cc.instantiate(this.tag);
                this.node.addChild(node);
                this.chilrens[i] = node;
            }
            var tag = this.chilrens[i];
            tag.x = p.x;
            tag.y = p.y
        }
        this.pathCanvas.updateTag(this.controlId,f);
    },

    deleteTag(){
        this.points.splice(this.controlId,1);
        var no = this.chilrens[this.controlId];
        no.destroy();
        this.chilrens.splice(this.controlId,1);
        this.pathCanvas.deleteTag(this.controlId);
    },
    runClickAction(i){
        this.chilrens[i].runAction(cc.sequence(cc.scaleTo(0.1,1.2),cc.delayTime(0.1),cc.scaleTo(0.1,1.0)));
    },

    onTouch(event,d){
        // cc.log(event.touch.getLocation());
        var p = event.touch.getLocation()
        if(event.type == cc.Node.EventType.TOUCH_START){
            for(var i=0; i<this.points.length; i++){
                var cp = this.points[i];
                if(Math.abs(p.x-cp.x) < 10 && Math.abs(p.y-cp.y)<10){
                    this.isControlTag = true;
                    this.isTouchMove = false;
                    this.controlId = i;
                    this.controlTime = new Date().getTime();
                    this.chilrens[i].scale = 1.2
                    event.stopPropagation();
                    break;
                }
            }
            return;
        }
        if(event.type == cc.Node.EventType.TOUCH_MOVE){
            if(this.isControlTag){
                this.isTouchMove = true;
                this.points[this.controlId] = p;
                this.updateTag(0)
            }
            return;
        }
        if(this.isControlTag){
            this.chilrens[this.controlId].scale = 1.0
            if(!this.isTouchMove && new Date().getTime() - this.controlTime > 1500){
                this.deleteTag();
            }
            if(this.isTouchMove){
                this.updateTag(2)
            } else {
                this.updateTag(3)
            }
            
            this.isControlTag = false;
            return;
        }
        this.points.push(p);
        this.controlId = this.points.length-1
        this.updateTag(1)
    },
});
